package com.kczyjg.hybird

import android.content.Context

object RotationManagerUtil {
    private var mRotationManager: RotationManager? = null
    private var mIsDirectionOpening = false
    private var yaw = 0f
    private var pitch = 0f
    private var roll = 0f
    private var accuracy = 0

    fun start(context: Context) {
        mRotationManager = RotationManager(context)
        mRotationManager?.rotationListener =
            RotationManager.RotationListener { yaw, pitch, roll, accuracy, rollReal ->
                this.yaw = yaw
                this.pitch = pitch
                this.roll = rollReal
                this.accuracy = accuracy
            }
        mRotationManager?.start()
        mIsDirectionOpening = true
    }

    fun resume() {
        if (!mIsDirectionOpening) {
            mRotationManager?.start()
        }
    }

    fun pause() {
        if (mIsDirectionOpening) {
            mRotationManager?.cancel()
        }
    }

    fun stop() {
        mRotationManager?.cancel()
        mIsDirectionOpening = false
    }

    fun destroy() {
        mRotationManager?.cancel()
        mRotationManager?.rotationListener = null
        mRotationManager = null
        mIsDirectionOpening = false
    }

    fun getYaw(): Float {
        return yaw
    }
}